package com.dbg.ui.table.model;

import com.dbg.entity.Language;

import javax.swing.table.AbstractTableModel;
import java.util.Iterator;
import java.util.List;

public // 表格模型
class SelectedTableModel extends AbstractTableModel {

    private List<Language> languageList;
    private final String[] columnNames = {"位序", "ID", "语言/框架", "扩展名", "升序", "降序", "删除"};
    private Object[][] data;

    public void setLanguageList(List<Language> languageList) {
        this.languageList = languageList;
    }

    public List<Language> getLanguageList() {
        return languageList;
    }

    public Language getLanguageAt(int rowIndex) {
        return languageList.get(rowIndex);
    }

    public void addLanguage(Language language) {
        this.languageList.add(language);
    }

    public void deleteLanguage(int index) {
        this.languageList.remove(index);
    }

    public SelectedTableModel(List<Language> languageList) {
        this.languageList = languageList;
        updateData();
    }

    public void updateData() {

        System.out.println("---------------------");
        for (Language language : languageList) {
            System.out.println(language);
        }

        data = new Object[languageList.size()][7];
        Iterator<Language> iterator = languageList.iterator();
        int row = 0;
        while (iterator.hasNext()) {
            Language lang = iterator.next();
            data[row][1] = lang.getId();
            data[row][2] = lang.getName();
            data[row][3] = lang.getFileName();
            data[row][4] = columnNames[4];     // 上升按钮
            data[row][5] = columnNames[5];   // 下降按钮
            data[row][6] = columnNames[6]; // 删除按钮
            data[row][0] = ++ row; // 位序
        }
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 3 || columnIndex == 4 || columnIndex == 5; // 只允许编辑按钮列
    }
}